/*
package com.itheima.aspect;

import com.itheima.mapper.EmpLoginLogMapper;
import com.itheima.pojo.*;
import com.itheima.pojo.LoginRequest;
import com.itheima.pojo.LoginResult;
import com.itheima.util.TliasException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
@Aspect
public class RecordEmpLoginAdvice {

    @Autowired
    private EmpLoginLogMapper empLoginLogMapper;

    @Around("execution(* com.itheima.service.impl.EmpServiceImpl.login(..))")
    public Object loginAdvice(ProceedingJoinPoint pjp) throws Throwable {

        Object[] args = pjp.getArgs();
        LoginRequest empDto = (LoginRequest) args[0];
        EmpLoginLog empLoginLog = new EmpLoginLog();
        empLoginLog.setUsername(empDto.getUsername());
        empLoginLog.setPassword(empDto.getPassword());
        empLoginLog.setLoginTime(LocalDateTime.now());

        long begin = System.currentTimeMillis();
        long end = 0;
        Object result =null;
        try {
            result = pjp.proceed();
            end = System.currentTimeMillis();

            empLoginLog.setIsSuccess((short) 1);
            LoginResult loginInfo =  (LoginResult) result;
            empLoginLog.setJwt(loginInfo.getToken());
            empLoginLog.setCostTime(end-begin);

            empLoginLogMapper.save(empLoginLog);

            return result;
        } catch (Throwable e) {
            end = System.currentTimeMillis();
            empLoginLog.setIsSuccess((short) 0);
            empLoginLog.setJwt(null);
            empLoginLog.setCostTime(end-begin);

            empLoginLogMapper.save(empLoginLog);

           // throw new TliasException("用户名或密码错误");
            return Result.error("用户名或密码错误");
        }


    }
}
*/

/*package com.itheima.aspect;

import com.itheima.mapper.EmpLoginLogMapper;
import com.itheima.pojo.LoginRequest;
import com.itheima.pojo.EmpLoginLog;
import com.itheima.pojo.LoginResult;
import com.itheima.pojo.Result;
import com.itheima.util.TliasException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
@Aspect
public class RecordEmpLoginAdvice {

    @Autowired
    private EmpLoginLogMapper empLoginLogMapper;

    @Around("execution(* com.itheima.service.impl.EmpServiceImpl.login(..))")
    public Object loginAdvice(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        LoginRequest loginRequest = (LoginRequest) args[0];
        EmpLoginLog empLoginLog = new EmpLoginLog();
        empLoginLog.setUsername(loginRequest.getUsername());
        empLoginLog.setPassword(loginRequest.getPassword());
        empLoginLog.setLoginTime(LocalDateTime.now());

        long begin = System.currentTimeMillis();
        long end = 0;
        Object result = null;

        try {
            result = pjp.proceed();
            end = System.currentTimeMillis();

            // 正确处理返回结果
            if (result instanceof Result) {
                Result response = (Result) result;
                if (response.getCode() == 1 && response.getData() instanceof LoginResult) {
                    // 登录成功
                    empLoginLog.setIsSuccess((short) 1);
                    LoginResult loginResult = (LoginResult) response.getData();
                    empLoginLog.setJwt(loginResult.getToken());
                } else {
                    // 登录失败
                    empLoginLog.setIsSuccess((short) 0);
                    empLoginLog.setJwt(null);
                }
            } else {
                // 未知情况，默认为失败
                empLoginLog.setIsSuccess((short) 0);
                empLoginLog.setJwt(null);
            }

            empLoginLog.setCostTime(end - begin);
            empLoginLogMapper.save(empLoginLog);

            return result;
        } catch (Throwable e) {
            end = System.currentTimeMillis();
            empLoginLog.setIsSuccess((short) 0);
            empLoginLog.setJwt(null);
            empLoginLog.setCostTime(end - begin);

            empLoginLogMapper.save(empLoginLog);
            throw new TliasException("用户名或密码错误");
            // 重新抛出异常，让上层处理
           // throw e;
        }
    }
}*/

/*

package com.itheima.aspect;

import com.itheima.mapper.EmpLoginLogMapper;
import com.itheima.pojo. LoginRequest;
import com.itheima.pojo.EmpLoginLog;
import com.itheima.pojo. LoginResult;
import com.itheima.pojo.LoginRequest;
import com.itheima.pojo.LoginResult;
import com.itheima.util.TliasException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
@Aspect
public class RecordEmpLoginAdvice {

    @Autowired
    private EmpLoginLogMapper empLoginLogMapper;

    @Around("execution(* com.itheima.service.impl.EmpServiceImpl.login(..))")
    public Object loginAdvice(ProceedingJoinPoint pjp) throws Throwable {

        Object[] args = pjp.getArgs();
        LoginRequest loginRequest = (LoginRequest) args[0];
        EmpLoginLog empLoginLog = new EmpLoginLog();
        empLoginLog.setUsername(loginRequest.getUsername());
        empLoginLog.setPassword(loginRequest.getPassword());
        empLoginLog.setLoginTime(LocalDateTime.now());

        long begin = System.currentTimeMillis();
        long end = 0;
        Object result =null;
        try {
            result = pjp.proceed();
            end = System.currentTimeMillis();

            empLoginLog.setIsSuccess((short) 1);
            LoginResult loginResult =  ( LoginResult) result;
            empLoginLog.setJwt(loginResult.getToken());
            empLoginLog.setCostTime(end-begin);

            empLoginLogMapper.save(empLoginLog);

            return result;
        } catch (Throwable e) {
            end = System.currentTimeMillis();
            empLoginLog.setIsSuccess((short) 0);
            empLoginLog.setJwt(null);
            empLoginLog.setCostTime(end-begin);

            empLoginLogMapper.save(empLoginLog);

            throw new TliasException("用户名或密码错误");
        }


    }


}
*/
/*
package com.itheima.aspect;

import com.itheima.mapper.EmpLoginLogMapper;
import com.itheima.pojo.LoginRequest;
import com.itheima.pojo.EmpLoginLog;
import com.itheima.pojo.LoginResult;
import com.itheima.pojo.Result;
import com.itheima.util.TliasException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
@Aspect
public class RecordEmpLoginAdvice {

    @Autowired
    private EmpLoginLogMapper empLoginLogMapper;

    @Around("execution(* com.itheima.service.impl.EmpServiceImpl.login(..))")
    public Object loginAdvice(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        LoginRequest loginRequest = (LoginRequest) args[0];
        EmpLoginLog empLoginLog = new EmpLoginLog();
        empLoginLog.setUsername(loginRequest.getUsername());
        empLoginLog.setPassword(loginRequest.getPassword());
        empLoginLog.setLoginTime(LocalDateTime.now());

        long begin = System.currentTimeMillis();
        long end = 0;
        Object result = null;

        try {
            result = pjp.proceed();
            end = System.currentTimeMillis();

            // 正确处理返回结果
            if (result instanceof Result) {
                Result response = (Result) result;
                if (response.getCode() == 1 && response.getData() instanceof LoginResult) {
                    // 登录成功
                    empLoginLog.setIsSuccess((short) 1);
                    LoginResult loginResult = (LoginResult) response.getData();
                    empLoginLog.setJwt(loginResult.getToken());
                } else {
                    // 登录失败
                    empLoginLog.setIsSuccess((short) 3);
                    empLoginLog.setJwt(null);
                }
            } else {
                // 未知情况，默认为失败
                Result response = (Result) result;
                empLoginLog.setIsSuccess((short) 2);
                LoginResult loginResult = (LoginResult) response.getData();
                empLoginLog.setJwt(loginResult.getToken());
            }

            empLoginLog.setCostTime(end - begin);
            empLoginLogMapper.save(empLoginLog);

            return result;
        } catch (Throwable e) {
            end = System.currentTimeMillis();
            empLoginLog.setIsSuccess((short) 0);
            empLoginLog.setJwt(null);
            empLoginLog.setCostTime(end - begin);

            empLoginLogMapper.save(empLoginLog);

            // 返回错误结果而不是抛出异常
            return Result.error("用户名或密码错误");
        }
    }
}
*/
package com.itheima.aspect;

import com.itheima.mapper.EmpLoginLogMapper;
import com.itheima.pojo.LoginRequest;
import com.itheima.pojo.EmpLoginLog;
import com.itheima.pojo.LoginResult;
import com.itheima.pojo.Result;
import com.itheima.util.TliasException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
@Aspect
public class RecordEmpLoginAdvice {

    @Autowired
    private EmpLoginLogMapper empLoginLogMapper;

    @Around("execution(* com.itheima.service.impl.EmpServiceImpl.login(..))")
    public Object loginAdvice(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        LoginRequest loginRequest = (LoginRequest) args[0];
        EmpLoginLog empLoginLog = new EmpLoginLog();
        empLoginLog.setUsername(loginRequest.getUsername());
        empLoginLog.setPassword(loginRequest.getPassword());
        empLoginLog.setLoginTime(LocalDateTime.now());

        long begin = System.currentTimeMillis();
        long end = 0;
        Object result = null;

        try {
            result = pjp.proceed();
            end = System.currentTimeMillis();

            // 正确处理返回结果
            if (result instanceof Result) {
                Result response = (Result) result;
                if (response.getCode() == 1 && response.getData() instanceof LoginResult) {
                    // 登录成功
                    empLoginLog.setIsSuccess((short) 1);
                    LoginResult loginResult = (LoginResult) response.getData();
                    empLoginLog.setJwt(loginResult.getToken());
                } else {
                    // 登录失败
                    empLoginLog.setIsSuccess((short) 0);
                    empLoginLog.setJwt(null);
                }
            } else {
                // 未知情况，默认为失败
                empLoginLog.setIsSuccess((short) 0);
                empLoginLog.setJwt(null);
            }

            empLoginLog.setCostTime(end - begin);
            empLoginLogMapper.save(empLoginLog);

            return result;
        } catch (Throwable e) {
            end = System.currentTimeMillis();
            empLoginLog.setIsSuccess((short) 0);
            empLoginLog.setJwt(null);
            empLoginLog.setCostTime(end - begin);

            empLoginLogMapper.save(empLoginLog);

            // 返回错误结果而不是抛出异常
            return Result.error("用户名或密码错误");
        }
    }
}
